package com.hfi.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ChangLiang
 * @date 2019/6/20
 */
//@Component
@WebFilter(filterName = "MyGetRequestFilter", urlPatterns = "/jpademo/getEmployeeById2")
public class MyGetRequestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        // 区分方法是POST还是GET
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        Map<String,String[]> m = new HashMap<String,String[]>(req.getParameterMap());
        if (m.keySet().contains("id")) {
            if (Integer.valueOf(m.get("id")[0]) > 10) {
                m.put("id", new String[]{"1"});
            }
        }
        MyGetHttpServletRequestWrapper requestWrapper = new MyGetHttpServletRequestWrapper(req,m);
        chain.doFilter(requestWrapper,resp);
    }

    @Override
    public void destroy() {

    }
}
